from pydantic import BaseModel, EmailStr, Field
from datetime import datetime
from typing import Optional

# 基础模型（共享字段）
class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=50)
    email: EmailStr
    full_name: Optional[str] = Field(None, max_length=100)

# 创建用户时的输入模型
class UserCreate(UserBase):
    password: str = Field(..., min_length=5)  # 明文密码（实际会加密存储）

# 更新用户时的输入模型（所有字段可选）
class UserUpdate(BaseModel):
    username: Optional[str] = Field(None, min_length=3, max_length=50)
    email: Optional[EmailStr] = None
    full_name: Optional[str] = Field(None, max_length=100)
    password: Optional[str] = Field(None, min_length=5)

# 数据库模型对应的返回模型（包含ID和时间戳）
class User(UserBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True  # 支持从ORM模型转换
